package com.ruoyi.study.common;

import cn.hutool.http.HTMLFilter;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.utils.MapDataUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.util.HtmlUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * @author chenjuncai
 */
@Slf4j
public class SendMsgUtil {
    private final static String AGENCYID="2132";
    /**
     * 帐户名称
     */
    private final static String ACCOUNT="E0328";
    /**
     * 密码
     */
    private final static String PASSWORD="112215";
    /**
     * 接口服务器IP地址
     */
    private final static String IpAddr="124.172.234.157:8180";
    public static boolean send(String phone,String content) throws UnsupportedEncodingException {
       StringBuilder sb = new StringBuilder();

        sb.append("【新媒体】：验证码：").append(content).append("请不要把验证码泄露给其他人,有效时间为5分钟");
        String postData = "Id="+AGENCYID+"&Name="+ACCOUNT+"&Psw="+PASSWORD+"&Message="+URLEncoder.encode(sb.toString(),"utf-8")+"&Phone="+phone+"&Timestamp=0";
        String s = sMS(postData, "http://"+IpAddr+"/service.asmx/SendMessageStr");
        String[] split = s.split(",");
        HashMap<String, Object> objectHashMap = new HashMap<>(10);
        for (String s1 : split) {
            String[] split1 = s1.split(":");
            objectHashMap.put(split1[0],split1[1]);
        }
        return objectHashMap.get("State").equals("1");
    }


    public static String sMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();
            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        send("19195754658","11111");
    }
}
